# =============================================================================
# Zara Albright
# Replication File for Paper: The Political and Pragmatic Determinants of Chinese 
# Development Finance in Latin America and the Caribbean, 2008-2019
# LAST REVIEWED 3 DEC 2024 
# =============================================================================

# Required packages =====
library(lme4)
library(tidyverse)
library(plyr)
library(reshape2)
library(foreign)
library(labelled)
library(lubridate)
library(haven)
library(ggplot2)
library(ggpubr)
library(ggeffects)
library(effects)
library(stargazer)
library(showtext)


# Load data =================================
replication_data <- read_csv(file.choose())

# Main Models (1-3) ============================================================

# Model 1: Economic Vector
mod1 <- glmer(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled + 
                (1 | country_ID), 
              family=binomial('logit'), 
              data=replication_data)

# Model 2: Political Vector (With Economic Base)
mod2 <- glmer(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled + 
                delt_op_US + lr_lean + idealdist_diff_US + lag_BoP_CH_pct +
                (1 | country_ID), 
              family=binomial('logit'), 
              data=replication_data)

# Model 3: Project Vector (With Economic Base)
mod3 <- glmer(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled + 
                amount_BB + grace_period + interest_rate + infrastructure + 
                (1 | country_ID), 
              family=binomial('logit'), 
              data=replication_data)
##

# Figure 1: Chinese and World Bank Lending to LAC, 2008-2019 ===================

# Number of Loans over Time 
plot.NumLoans <- ggplot(replication_data, aes(x=year, color=lender)) +
  geom_bar(stat='count',position='dodge', aes(x=year, color=lender, fill=lender)) +
  scale_color_manual(values=c('black','gray')) +
  scale_x_continuous(limits=c(2008,2019), breaks=seq(2008,2019,1)) +
  labs(y="Number of Loans", x="") +
  theme_light() +
  theme(text=element_text(size=24), 
        legend.position=c(0.89,0.85), legend.title = element_blank()) + 
  scale_fill_manual(values=c('black','gray'))


# Average Sizes of Loans over Time 
tab.LoanSummaries <- replication_data %>%
  group_by(year, lender) %>%
  dplyr::summarize(meansize = mean(amount, na.rm=TRUE))
plot.SizeLoans <- ggplot(tab.LoanSummaries, aes(x=year, color=lender)) +
  geom_point(aes(y=meansize), size=3) +
  geom_line(aes(y=meansize), size=1) +
  scale_color_manual(values=c('black','gray')) +
  scale_x_continuous(limits=c(2008,2019), breaks=seq(2008,2019,1)) +
  labs(y="Avg. Loan Size (US$ MM)", x="Year") +
  theme_light() +
  theme(text=element_text(size=24), 
        legend.position=c(0.89,0.85), legend.title = element_blank()) 

Fig1 <- ggarrange(plot.NumLoans, plot.SizeLoans, nrow = 2)


# Table 2: Independent Variable Summary Statistics =============================

tab.IVs <- replication_data %>%
  select(GDPC_cUS, inf_rate, governance, totaltradeGDP, sovCR_scaled,
         delt_op_US, lr_lean, idealdist_diff_US, BoP_CH_pct,
         amount, grace_period, interest_rate, infrastructure)
tab2 <- stargazer(data.frame(tab.IVs),
                  title='Table 2: Summary Statistics for Independent Variables',
                  type='text')


# Table 3: Logistic Regression Results =========================================

tab.ModResults <- stargazer(mod1,mod2,mod3,
                            title = 'Table 3: Results of Likelihood Models of Lender',
                            style = 'apsr',
                            column.labels = c('Economic','Political','Project'),
                            model.numbers = FALSE,
                            covariate.labels = c('log(GDP Per Capita)','Inflation','Governance','Trade with China %GDP','Sov. Credit Risk','∆ Opinion - US','Party Left-Right Lean','∆ UN Vote Distance - US', 'China Relative Power','Loan Amount (Scaled)','Grace Period','Interest Rate','Infrastructure','Constant'),
                            dep.var.labels = c('Lender (China=1, WB=0)'),
                            align = TRUE,
                            label = 'mod_results',
                            notes.align = 'l', 
                            type='text'
)
##

# Figure 2: Economic Results (Model 1) Predicted Probabilities =================

# GDP per capita
mod1_PP <- ggeffect(mod1, 'lag_GDPC_log [all]')
plot.GDPC <- ggplot(mod1_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="log(GDP Per Capita)", y="Predicted Probability") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'), 
        legend.position=c(0.89,0.85), legend.title = element_blank())

# Inflation
mod1_PP <- ggeffect(mod1, 'lag_inf_rate [all]')
plot.Inflation <- ggplot(mod1_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Inflation Rate",y="") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

#Governance 
mod1_PP <- ggeffect(mod1, 'lag_governance [all]')
plot.Governance <- ggplot(mod1_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Governance", y="Predicted Probability") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

# Sovereign Credit Risk
mod1_PP <- ggeffect(mod1, 'sovCR_scaled [all]')
plot.SovCR <- ggplot(mod1_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Sov. Credit Risk", y="") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

Fig2 <- ggarrange(plot.GDPC, plot.Inflation, plot.Governance, plot.SovCR, ncol=2, nrow=2)


# Figure 3: Political Results (Model 2) Predicted Probabilities ================

# Party Left-Right Lean
mod2_PP <- ggeffect(mod2, 'lr_lean [all]')
plot.LeftRight <- ggplot(mod2_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Party Left-Right Lean", y="Predicted Probability") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

# UN Vote Trend 
mod2_PP <- ggeffect(mod2, 'idealdist_diff_US [all]')
plot.UNvote <- ggplot(mod2_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="∆ Policy Distance from US", y="") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

mod2_PP <- ggeffect(mod2, 'lag_BoP_CH_pct [all]')
plot.BoP <- ggplot(mod2_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="China Relative Power", y="") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

Fig3 <- ggarrange(plot.LeftRight, plot.UNvote, plot.BoP, ncol=3)


# Figure 4: Project Results (Model 3) Predicted Probabilities  =================

# Loan Amount 
mod3_PP <- ggeffect(mod3, 'amount_BB [all]')
plot.Amount <- ggplot(mod3_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Loan Amount (US$ BB)", y="Predicted Probability") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

# Grace Period
mod3_PP <- ggeffect(mod3, 'grace_period [all]')
plot.Grace <- ggplot(mod3_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Grace Period", y="") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

# Interest Rate
mod3_PP <- ggeffect(mod3, 'interest_rate [all]')
plot.Interest <- ggplot(mod3_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Interest Rate", y="Predicted Probability") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

mod3_PP <- ggeffect(mod3, 'infrastructure [all]')
plot.Infra <- ggplot(mod3_PP, aes(x=x, y=predicted)) +
  geom_line(size=1) +
  geom_ribbon(aes(ymin=conf.low, ymax=conf.high), alpha=0.1) +
  labs(x="Infrastructure", y="") +
  scale_y_continuous(limits=c(0,1)) +
  theme_light() +
  theme(text=element_text(size=24, family='arial'),
        legend.position=c(0.89,0.85), legend.title = element_blank())

Fig4 <- ggarrange(plot.Amount, plot.Grace, plot.Interest, plot.Infra, ncol=2, nrow=2)


# Appendix: Single-Level Binomial Model Robustness Checks ======================

mod1_CFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                  factor(country_ID),
                family=binomial('logit'),
                data=replication_data)
mod1_CYFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                  factor(country_ID) + factor(year),
                family=binomial('logit'),
                data=replication_data)
mod1_YFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                   factor(year),
                 family=binomial('logit'),
                 data=replication_data)

mod2_CFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                  delt_op_US + lr_lean + idealdist_diff_US + lag_BoP_CH_pct +
                  factor(country_ID),
                family=binomial('logit'),
                data=replication_data)
mod2_CYFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                   delt_op_US + lr_lean + idealdist_diff_US + lag_BoP_CH_pct +
                   factor(country_ID) + factor(year),
                 family=binomial('logit'),
                 data=replication_data)
mod2_YFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                   delt_op_US + lr_lean + idealdist_diff_US + lag_BoP_CH_pct +
                   factor(year),
                 family=binomial('logit'),
                 data=replication_data)

mod3_CFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                  amount_BB + grace_period + interest_rate + infrastructure +
                  factor(country_ID),
                family=binomial('logit'),
                data=replication_data)
mod3_CYFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                   amount_BB + grace_period + interest_rate + infrastructure +
                   factor(country_ID) + factor(year),
                 family=binomial('logit'),
                 data=replication_data)
mod3_YFE <- glm(binDV ~ lag_GDPC_log + lag_inf_rate + lag_governance + lag_totaltradeGDP + sovCR_scaled +
                  amount_BB + grace_period + interest_rate + infrastructure +
                  factor(year),
                 family=binomial('logit'),
                 data=replication_data)

# Model 1
stargazer(mod1_CFE,mod1_YFE,mod1_CYFE,
          title = 'Single-Level Fixed-Effects Models, Economic',
          style = 'apsr',
          column.labels = c('Country FE', 'Year FE', 'Country-Year FE'),
          model.numbers = FALSE,
          omit = c(7:33),
          covariate.labels = c('log(GDP Per Capita)','Inflation','Governance','Trade with China %GDP','Sov. Credit Risk',''),
          dep.var.labels = c('Lender (China=1, WB=0)'),
          intercept.bottom = TRUE,
          align = TRUE,
          label = 'appendix1',
          notes.align = 'l', 
          type='text')

# Model 2
stargazer(mod2_CFE,mod2_YFE,mod2_CYFE,
          title = 'Single-Level Fixed-Effects Models, Political',
          style = 'apsr',
          column.labels = c('Country FE', 'Year FE', 'Country-Year FE'),
          model.numbers = FALSE,
          omit = c(11:37),
          covariate.labels = c('log(GDP Per Capita)','Inflation','Governance','Trade with China %GDP','Sov. Credit Risk','∆ Opinion - US','Party Left-Right Lean','∆ UN Vote Distance - US', 'China Relative Power', ''),
          dep.var.labels = c('Lender (China=1, WB=0)'),
          align = TRUE,
          label = 'appendix2',
          notes.align = 'l', 
          type='text')

# Model 3
stargazer(mod3_CFE,mod3_YFE,mod3_CYFE,
          title = 'Single-Level Fixed-Effects Models, Project',
          style = 'apsr',
          column.labels = c('Country FE', 'Year FE', 'Country-Year FE'),
          model.numbers = FALSE,
          omit = c(11:37),
          covariate.labels = c('log(GDP Per Capita)','Inflation','Governance','Trade with China %GDP','Sov. Credit Risk','Loan Amount (Scaled)','Grace Period','Interest Rate','Infrastructure', ''),
          dep.var.labels = c('Lender (China=1, WB=0)'),
          align = TRUE,
          label = 'appendix3',
          notes.align = 'l', 
          type='text')
